package com.paireach.szwolaiyun.fragmentall;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;

import com.paireach.szwolaiyun.R;
import com.paireach.szwolaiyun.sz_common_widget.fragment.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import szwolaiyun.paireach.com.szwolaizhuang.model.business.ListItem;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.AppEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpErrorEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.event.HttpSuccessEvent;
import szwolaiyun.paireach.com.szwolaizhuang.model.requestTag.RequestTag;
import szwolaiyun.paireach.com.szwolaizhuang.net.WayBillRequest;

/**
 * 设置预计到达时间
 * Created by Administrator on 2016/7/4.
 * @deprecated app端无需再处理预计到达时间，统一由工厂方处理
 */
@Deprecated
public class SetForecastTimeFragment extends BaseFragment implements AdapterView.OnItemSelectedListener {
    public static final String TAG = "SetForecastTimeFragment";
    private View view;

    private Spinner spinner_month, spinner_time;
    private SpinnerAdapter monthAdapter, timeAdapter;

    /**
     * 订单号
     */
    private String orderCode;

    public static final SetForecastTimeFragment getInstance(String orderCode) {
        SetForecastTimeFragment fragment = new SetForecastTimeFragment();
        Bundle bundle = new Bundle();
        bundle.putString("orderCode", orderCode);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        monthAdapter = new SpinnerAdapter(getActivity());
        timeAdapter = new SpinnerAdapter(getActivity());
        orderCode = getArguments().getString("orderCode");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_setdate, null);
        spinner_month = (Spinner) view.findViewById(R.id.month);
        spinner_time = (Spinner) view.findViewById(R.id.time);
        spinner_month.setOnItemSelectedListener(this);
        return view;
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        spinner_month.setAdapter(monthAdapter);
        spinner_time.setAdapter(timeAdapter);
        showProgressDialog();
        new WayBillRequest().获取运单可用的排班日期(orderCode);
    }

    /**
     * 处理网络失败/错误请求
     * <p>直接判断HttpEvent的RequestTag即可
     *
     * @param event 错误事件
     */
    @Override
    public void httpErrorEvent(HttpErrorEvent event) {
        super.httpErrorEvent(event);
        if(event.getRequestTag()== RequestTag.获取运单可用的排班日期){
            dismissProgressDialog();
            showToast(event.getErrorMessage());
        }
    }

    /**
     * 处理网络成功请求
     * <p>直接判断HttpEvent的RequestTag即可
     *
     * @param event 成功事件
     */
    @Override
    public void httpSuccessEvent(HttpSuccessEvent event) {
        super.httpSuccessEvent(event);
        if(event.getRequestTag()== RequestTag.获取运单可用的排班日期){
            dismissProgressDialog();
            Log.d("","");
        }
    }

    /**
     * 处理app内部事件
     *
     * @param event app内部事件
     */
    @Override
    public void applicationEvent(AppEvent event) {
        super.applicationEvent(event);
    }

    /**
     * 选中日期下拉框的回调方法
     * @param adapterView
     * @param view
     * @param i
     * @param l
     */
    @Override
    public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {

    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) {

    }

    private class SpinnerAdapter extends BaseAdapter {

        private List<ListItem> list;
        private Context mContext;

        public SpinnerAdapter(Context mContext) {
            list = new ArrayList<>();
            this.mContext = mContext;
        }

        public void setList(List<ListItem> list) {
            this.list.clear();
            this.list.addAll(list);
            notifyDataSetChanged();
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public ListItem getItem(int i) {
            return list.get(i);
        }

        @Override
        public long getItemId(int i) {
            return i;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            ListItem item = list.get(i);
            H h;
            if (view == null) {
                h = new H();
                view = LayoutInflater.from(mContext).inflate(android.R.layout.simple_dropdown_item_1line, viewGroup, false);
                h.textView = (TextView) view.findViewById(android.R.id.text1);
                view.setTag(h);
            } else {
                h = (H) view.getTag();
            }
            h.textView.setText(item.text);
            return view;
        }

        class H {
            TextView textView;
        }
    }
}
